package com.cyrus.vueboot.controller.dpt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyrus.vueboot.common.R;
import com.cyrus.vueboot.error.BusinessError;
import com.cyrus.vueboot.error.CommonError;
import com.cyrus.vueboot.pojo.dpt.Department;
import com.cyrus.vueboot.service.DepartmentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/24
 */
@RestController
@Slf4j
@Api
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @PostMapping("/search")
    public R search(Integer page, Integer size, String key) {
        IPage iPage=departmentService.search(page, size, key);
        List records = iPage.getRecords();
        long total = iPage.getTotal();
        long size1 = iPage.getSize();
        return R.ok().data("records", records).data("total", total).data("size",size1);
    }

    @PostMapping("/findAlldpt")
    public R findAlldpt() {
        List<Department> list = departmentService.list();
        return R.ok().data("list", list);
    }

    @PostMapping("/delDepartments")
    public R delDepartments(Long[] ids) {
        try {
            departmentService.delDepartments(ids);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10002.getCode(), CommonError.E_10002.getMsg());
        }
    }

    @PostMapping("/findDetailById")
    public R findDetailById(Integer id) {

        Department detailById = departmentService.findDetailById(id);
        return R.ok().data("Department", detailById);
    }


    @PostMapping("/save")
    public R save(@RequestBody Department department) {
        try {
            departmentService.saveNewDpt(department);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10001.getCode(), CommonError.E_10001.getMsg());
        }
    }

    @PostMapping("/getAllOneLevel")
    public R getAllOneLevel() {
        List<Department> list = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getDptLevel, 1).eq(Department::getIsDelete, 0));
        return R.ok().data("AllOneLevel",list);
    }

    @PostMapping("/saveDetail")
    public R saveDetail(@RequestBody Department department) {
        try {
            departmentService.updateById(department);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10003.getCode(), CommonError.E_10003.getMsg());
        }
    }



}
